{extend name="base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>该页面展示了商城所有的求购单。</li>
			<li>可根据关键词等条件进行查询。</li>
		</ul>
	</div>
</div>

<!-- 搜索框 -->
<div class="ns-screen layui-collapse" lay-filter="selection_panel">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title"></h2>
		<form class="layui-colla-content layui-form layui-show"  lay-filter="order_list">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">搜索方式：</label>
					<div class="layui-input-inline">
						<select name="search_type">
							<option value="order_no">订单号</option>
							<option value="out_trade_no">外部单号</option>
							<option value="buyer_name">用户</option>
						</select>
					</div>
					<div class="layui-input-inline">
						<input type="text" name="search_text" autocomplete="off" class="layui-input" />
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">下单时间：</label>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" name="start_time" placeholder="开始时间" id="start_time" readonly>
						<i class="ns-calendar"></i>
					</div>
					<div class="layui-form-mid">-</div>
					<div class="layui-input-inline">
						<input type="text" class="layui-input" name="end_time" placeholder="结束时间" id="end_time" readonly>
						<i class="ns-calendar"></i>
					</div>
					<button class="layui-btn layui-btn-primary date-picker-btn" onclick="datePick(7, this);return false;">近7天</button>
					<button class="layui-btn layui-btn-primary date-picker-btn" onclick="datePick(30, this);return false;">近30天</button>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">订单类型：</label>
					<div class="layui-input-inline">
						<select name="order_type" lay-filter="order_type">
							<option value="">全部</option>
							{foreach $order_type_list as $k=>$v}

							<option value="{$k}" {if $k == $order_type}selected{/if}>{$v}</option>
							{/foreach}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">付款方式：</label>
					<div class="layui-input-inline">
						<select name="pay_type">
							<option value="">全部</option>
							{foreach $pay_type_list as $k=>$v}
							<option value="{$k}">{$v}</option>
							{/foreach}
						</select>
					</div>
				</div>
			</div>
			<div class="ns-form-row">
				<button class="layui-btn ns-bg-color" lay-submit id="btn_search" lay-filter="btn_search">筛选</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</form>
	</div>
</div>

<div class="layui-tab ns-table-tab" lay-filter="order_tab">
	<ul class="layui-tab-title">
		<li class="layui-this" data-status="">全部</li>
		{foreach $order_status as $k=>$v}
		<li data-status="{$k}">{$v}</li>
		{/foreach}
	</ul>
	<div class="layui-tab-content">
		<!-- 列表 -->
		<table id="order_list" lay-filter="order_list"></table>
	</div>
</div>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="ns-table-btn">
        {{# if(d.order_status == 2) { }}
        <a class="layui-btn" lay-event="pay_info">支付信息</a>
        {{# } }}
		<a class="layui-btn" lay-event="detail">详情</a>
	</div>
</script>
{/block}
{block name="script"}
<script>
	var table, form, laytpl, element, layer_pass, repeat_flag = false; //防重复标识
	layui.use(['form', 'laytpl','laydate','element'], function() {
		form = layui.form;
		laytpl = layui.laytpl;
		element = layui.element;
        laydate = layui.laydate;
		form.render();

		//监听Tab切换，以改变地址hash值
		element.on('tab(order_tab)', function() {
			table.reload({
				page: {
					curr: 1
				},
				where: {
					'order_status': this.getAttribute('data-status'),
				}
			});
		});

        //渲染时间
        laydate.render({
            elem: '#start_time'
            ,type: 'datetime'
            ,change: function(value, date, endDate){
                $(".date-picker-btn").removeClass("selected");
            }
        });
        laydate.render({
            elem: '#end_time'
            ,type: 'datetime'
            ,change: function(value, date, endDate){
                $(".date-picker-btn").removeClass("selected");
            }
        });

		table = new Table({
			elem: '#order_list',
			url: ns.url("admin/sysorder/lists"),
			where : {order_type : "{$order_type}"},
			cols: [
				[{
					field: 'order_no',
					title: '订单号',
					width: '12%',
					unresize: 'false'
				}, {
					field: 'out_trade_no',
					title: '外部交易号',
					width: '12%',
					unresize: 'false',
				}, {
					field: 'order_type_name',
					title: '订单类型',
					width: '8%',
					unresize: 'false',
				}, {
					field: 'order_name',
					title: '服务名称',
					width: '10%',
					unresize: 'false',
				}, {
					field: 'buyer_name',
					title: '用户',
					width: '8%',
					unresize: 'false',
				}, {
					field: 'amount',
					title: '订单金额',
					width: '10%',
					align: 'right',
					unresize: 'false',
					templet: function (data) {
						return '￥' + data.order_money;
					}
				}, {
					field: 'pay_type_name',
					title: '支付方式',
					width: '9%',
					unresize: 'false',
				},{
					field: 'create_time',
					title: '创建时间',
					width: '12%',
					unresize: 'false',
					templet: function (data) {
						return  ns.time_to_date(data.create_time);
					}
				}, {
					field: 'order_status_name',
					title: '订单状态',
					width: '9%',
					unresize: 'false',
				}, {
					title: '操作',
					toolbar: '#operation',
					unresize: 'false',
					width: '10%'
				}]
			],
		});

		/**
		 * 监听工具栏操作
		 */
		table.tool(function(obj) {
			var data = obj.data;
			switch (obj.event) {
				case 'detail':
					location.href = ns.url("admin/sysorder/detail?order_id=" + data.order_id);
					break;
                case 'pay_info':
					location.href = ns.url("admin/sysorder/status?order_id=" + data.order_id);
                    break;
			}
		});
		
		/**
		 * 搜索功能
		 */
		form.on('submit(btn_search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
			return false;
		});

		function offlinePay(data) {
            if (repeat_flag) return false;
            repeat_flag = true;
            layer.confirm('确定用户已线下支付吗?', function() {
                $.ajax({
                    type: 'POST',
                    dataType: 'JSON',
                    url: ns.url("admin/sysorder/offlinepay"),
                    data: {order_id: data.order_id},
                    success: function(res) {
                    	
                        layer.msg(res.message);
                        repeat_flag = false;

                        if (res.code == 0) {
                            table.reload();
                        } else {
                            location.reload();
                        }
                    }
                });
            }, function () {
                layer.close();
                repeat_flag = false;
            });
        }

	});
    /**
     * 七天时间
     */
    function datePick(date_num,event_obj){
        $(".date-picker-btn").removeClass("selected");
        $(event_obj).addClass('selected');
        // alert(new Date().format("yyyy-MM-dd hh:mm"));
        var now_date = new Date();

        Date.prototype.Format = function (fmt,date_num) { //author: meizz
            this.setDate(this.getDate()-date_num);
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "H+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
        // var now_time =  new Date().Format("yyyy-MM-dd HH:mm:ss",0);//当前日期
        var now_time =  new Date().Format("yyyy-MM-dd 23:59:59",0);//当前日期
        var before_time =  new Date().Format("yyyy-MM-dd 00:00:00",date_num-1);//前几天日期
        $("input[name=start_time]").val(before_time,0);
        $("input[name=end_time]").val(now_time,date_num-1);

    }
</script>
{/block}